﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DOAN.DO;
using DOAN.BL;

namespace DOAN.Admin
{
    public partial class QL_Ykien : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["quyen"] != null)
            {

                if (Session["quyen"].ToString() == "AD")
                {
                    Response.Redirect("DangnhapAdmin.aspx");
                }
                else
                {

                    if (Session["Tendangnhap"] != null && Session["Tendangnhap"] != "")
                    {

                        MasterPage mt = this.Master as MasterPage;
                        LinkButton lb = mt.FindControl("LinkButton1") as LinkButton;
                        lb.Visible = false;
                        Label bl = mt.FindControl("lblDnthanhcong") as Label;
                        bl.Visible = true;
                        Label blten = mt.FindControl("lbtendn") as Label;
                        blten.Visible = true;
                        blten.Text = Session["Tendangnhap"].ToString();
                        LinkButton lbDangky = mt.FindControl("lbDangky") as LinkButton;
                        lbDangky.Visible = false;
                        LinkButton lbthoat = mt.FindControl("lbThoat") as LinkButton;
                        lbthoat.Visible = true;
                    }

                    if (!IsPostBack)
                    { napYkien(); }

                }

            }
            else
            {
                Response.Redirect("LoginAdmin.aspx");
            }
        }
        private void napYkien()
        {
            try
            {
                griYkien.DataSource = new YkienBL().SelectAll();
                griYkien.DataBind();
            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        protected void griYkien_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int ma = Convert.ToInt32(e.CommandArgument);
            int result = 0;
            try
            {
                if (e.CommandName == "xoa")
                {
                    result = new YkienBL().Delete(new YkienDO { Maykien = ma });
                    if (result == 0)
                    {
                        lblLoi.Text = "Xoa thanh cong !";
                        napYkien();
                    }
                    else
                    {
                        lblLoi.Text = "Xoa khong thang cong !";
                    }
                }
                if (e.CommandName == "xem")
                {
                    pnYkien.Visible = true;
                    YkienDO obj = new YkienBL().Select(new YkienDO { Maykien = ma });
                    txtNoidung.Text = obj.Noidung;
                    txtNoidung.Enabled = false;
                    txtTen.Text = obj.Tentaikhoan;
                    txtTen.Enabled = false;
                    txtEmail.Text = obj.Email;
                    txtEmail.Enabled = false;
                }
            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
                
            }
        }
        private void Tim_kiem(int trangthai, string tukhoa)
        {
            try
            {
                griYkien.DataSource = new YkienBL().TimKiem(trangthai, tukhoa);
                griYkien.DataBind();
            }
            catch (Exception ee)
            {
                lblLoi.Text = ee.Message;
            }
            finally
            {
            }
        }
        protected void imgtk_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["quyen"] != null)
            {
                if (txtTimkiem.Text == "")
                {
                    lblLoi.Text = "Bạn chưa nhập thông tin tìm kiếm";
                }
                else
                {
                    string ma = txtTimkiem.Text;
                    if (rd_Ykien.SelectedItem.Value == "0")
                    {
                        Tim_kiem(0, ma);
                    }
                    if (rd_Ykien.SelectedItem.Value == "1")
                    {
                        Tim_kiem(1, ma);
                    }
                }
            }
            else
            {
                Response.Redirect("LoginAdmin.aspx");
            }
        }
        protected void imgRe_Click(object sender, ImageClickEventArgs e)
        {
            napYkien();
        }
        protected void btLamlai_Click(object sender, EventArgs e)
        {
            pnYkien.Visible = false;
        }
    }
}
